package com.walle.concurrenttest.waitnotify;

import javax.sound.midi.Soundbank;
import java.util.Map;

/**
 * @author zhangjiapeng
 * @Package com.walle.concurrenttest.waitnotify
 * @Description: ${todo}
 * @date 2019/2/18 15:44
 */
public class Storage {

    private int num;

    private static final int MAX_COUNT = 100;

    public synchronized void remove(){
        while (num <= 0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String threadName = Thread.currentThread().getName();
        notifyAll();
        num--;
        System.out.println(threadName + "消费产品，当前数量："+num);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void add(){
        while (num >= MAX_COUNT){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String threadName = Thread.currentThread().getName();
        notifyAll();
        num++;
        System.out.println(threadName + "生产产品，当前数量："+num);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
